﻿package lobby
{
	import com.sickworks.components.DateChooserComp;
	import flash.events.Event;
	import lobby.exchangeddata.HashTable;
	import lobby.exchangeddata.HistoryInfoLobby;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import lobby.events.LobbyEvent;
	import lobby.common.Utility;
	
	/**
	 * ...
	 * @author Doan Nguyen Thuy Nha Truc (Bamboo)
	 */
	public class GameHistoryPanel extends MovieClip
	{
		public var dateChooser:DateChooserComp;
		public var dateFromIcon:MovieClip;
		public var dateToIcon:MovieClip;
		public var txtDateFromTi:TextField;
		public var txtDateToTi:TextField;
		public var alphaTextFrom:MovieClip;
		public var alphaTextTo:MovieClip;
		public var btnSearchReport:SimpleButton;
		public var btnCancelReport:SimpleButton;
		
		public var txtReportTitleTf:TextField;
		public var txtDatefromTf:TextField;
		public var txtDateToTf:TextField;
		public var txtOkTf:TextField;
		public var txtCloseTf:TextField;
		public var txtBetAmountTf:TextField;
		public var txtWinloseTf:TextField;
		public var txtRollingAmountTf:TextField;
		public var txtComTypeTf:TextField;
		public var txtComRateTf:TextField;
		public var txtComAmountTf:TextField;
		public var txtTotalTf:TextField;
		
		public var txtValueBetAmountTf:TextField;
		public var txtValueWinloseTf:TextField;
		public var txtValueRollingAmountTf:TextField;
		public var txtValueComTypeTf:TextField;
		public var txtValueComRateTf:TextField;
		public var txtValueComAmountTf:TextField;
		public var txtValueTotalTf:TextField;
		
		private var currentIconDate:MovieClip;
		private var dateFrom:Array;
		private var dateTo:Array;
		
		public function GameHistoryPanel()
		{
			hide();
			hideResult();
			dateFrom = new Array();
			dateTo = new Array();
			this.dateChooser.visible = false;
			this.txtOkTf.mouseEnabled = false;
			this.txtCloseTf.mouseEnabled = false;
			
			this.dateFromIcon.addEventListener(MouseEvent.CLICK, dateIconClickHdl);
			this.dateToIcon.addEventListener(MouseEvent.CLICK, dateIconClickHdl);
			this.alphaTextFrom.addEventListener(MouseEvent.CLICK, dateIconClickHdl);
			this.alphaTextTo.addEventListener(MouseEvent.CLICK, dateIconClickHdl);
			this.dateChooser.addEventListener(DateChooserComp.SELECTION_CHANGED, dateChooseClickHdl);
			this.btnSearchReport.addEventListener(MouseEvent.CLICK, searchReportHdl);
			this.btnCancelReport.addEventListener(MouseEvent.CLICK, cancelReportHdl);
			
		}
		
		public function show():void
		{
			this.visible = true;
		}
		
		public function hide():void
		{
			this.visible = false;
		}
		
		private function searchReportHdl(e:Event):void
		{
			//trace("searchReportHdl");
			if (txtDateFromTi.text == "" || txtDateToTi.text == "")
			{
				dispatchEvent(new LobbyEvent(LobbyEvent.SHOW_ERR_DATE_NULL, true));
				return;
			}
			var dateF:Date = new Date(dateFrom[0], dateFrom[1], dateFrom[2]);
			var dateT:Date = new Date(dateTo[0], dateTo[1], dateTo[2]);
			if (dateF < dateT)
				dispatchEvent(new LobbyEvent(LobbyEvent.GET_HISTORY, true));
			else
				dispatchEvent(new LobbyEvent(LobbyEvent.SHOW_ERR_CHOOSE_DATE, true));
		}
		
		private function cancelReportHdl(e:Event):void
		{
			hide();
		}
		
		private function dateChooseClickHdl(e:Event):void
		{
			this.dateChooser.visible = false;
			var dateSelect:String = e.target.selectedDates[0];
			var arrayDate:Array = dateSelect.split(" ");
			dateSelect = arrayDate[2] + "/" + arrayDate[1] + "/" + arrayDate[5];
			if (currentIconDate == dateToIcon)
			{
				txtDateToTi.text = dateSelect;
				dateTo[0] = arrayDate[5];
				dateTo[1] = Utility.getMonthNumber(arrayDate[1]);
				dateTo[2] = arrayDate[2];
				dispatchEvent(new LobbyEvent(LobbyEvent.UPDATE_DATE_TO, true));
			}
			else if (currentIconDate == dateFromIcon)
			{
				txtDateFromTi.text = dateSelect;
				dateFrom[0] = arrayDate[5];
				dateFrom[1] = Utility.getMonthNumber(arrayDate[1]);
				dateFrom[2] = arrayDate[2];
				dispatchEvent(new LobbyEvent(LobbyEvent.UPDATE_DATE_FROM, true));
			}
		}
		
		private function dateIconClickHdl(e:MouseEvent):void
		{
			if (e.currentTarget == dateToIcon || e.currentTarget == alphaTextTo)
			{
				this.dateChooser.y = this.dateToIcon.y - 10;
				this.dateChooser.visible = true;
				setChildIndex(dateChooser, this.numChildren - 1);
				currentIconDate = dateToIcon;
			}
			else if (e.currentTarget == dateFromIcon || e.currentTarget == alphaTextFrom)
			{
				this.dateChooser.y = this.dateFromIcon.y - 10;
				this.dateChooser.visible = true;
				setChildIndex(dateChooser, this.numChildren - 1);
				currentIconDate = dateFromIcon;
			}
		}
		
		public function initText(hashTableInfo:HashTable):void
		{
			this.txtReportTitleTf.text = hashTableInfo.lookupString("reportMsg");
			this.txtDatefromTf.text = hashTableInfo.lookupString("dFromMsg");
			this.txtDateToTf.text = hashTableInfo.lookupString("dToMsg");
			this.txtBetAmountTf.text = hashTableInfo.lookupString("betAmountMsg");
			this.txtWinloseTf.text = hashTableInfo.lookupString("winMsg") + ":";
			this.txtRollingAmountTf.text = hashTableInfo.lookupString("rollingMsg");
			this.txtComTypeTf.text = hashTableInfo.lookupString("cTypeMsg");
			this.txtComRateTf.text = hashTableInfo.lookupString("cRateMsg");
			this.txtComAmountTf.text = hashTableInfo.lookupString("cAmountMsg");
			this.txtTotalTf.text = hashTableInfo.lookupString("tAmountMsg");
			this.txtOkTf.text = hashTableInfo.lookupString("btnOK");
			this.txtCloseTf.text = hashTableInfo.lookupString("btnclose");
		
		}
		
		/**
		 * @author	Bamboo
		 * @param	dFrom
		 * @param	dTo
		 */
		public function updateDate(dFrom:String, dTo:String):void
		{
			this.txtDateFromTi.text = dFrom;
			this.txtDateToTi.text = dTo;
			var tempFrom:Array = dFrom.split("/");
			this.dateFrom[0] = tempFrom[2];
			this.dateFrom[1] = Utility.getMonthNumber(tempFrom[1]);
			this.dateFrom[2] = tempFrom[0];
			
			var tempTo:Array = dTo.split("/");
			this.dateTo[0] = tempTo[2];
			this.dateTo[1] = Utility.getMonthNumber(tempTo[1]);
			this.dateTo[2] = tempTo[0];
		}
		
		/**
		 * @author	Bamboo
		 * @param	info
		 */
		public function updateValueHistory(info:Object):void
		{
			var history:HistoryInfoLobby = HistoryInfoLobby(info);
			this.txtValueBetAmountTf.text = history.valueBetAmount;
			this.txtValueWinloseTf.text = history.valueWinlose;
			this.txtValueRollingAmountTf.text = history.valueRollingAmount;
			this.txtValueComTypeTf.text = history.valueComType;
			this.txtValueComRateTf.text = history.valueComRate;
			this.txtValueComAmountTf.text = history.valueComAmount;
			this.txtValueTotalTf.text = history.valueTotal;
			if (history.valueRollingAmount == "0")
				hideResult();
			else
				showResult();
		}
	
		/**
		 * @author	Tom
		 */
		private function hideResult():void
		{
			txtRollingAmountTf.visible = false;
			txtValueRollingAmountTf.visible = false;
			txtComTypeTf.visible = false;
			txtValueComTypeTf.visible = false;
			txtComRateTf.visible = false;
			txtValueComRateTf.visible = false;
			txtComAmountTf.visible = false;
			txtValueComAmountTf.visible = false;
			txtTotalTf.visible = false;
			txtValueTotalTf.visible = false;
		}
		
		/**
		 * @author	Tom
		 */
		private function showResult():void
		{
			txtRollingAmountTf.visible = true;
			txtValueRollingAmountTf.visible = true;
			txtComTypeTf.visible = true;
			txtValueComTypeTf.visible = true;
			txtComRateTf.visible = true;
			txtValueComRateTf.visible = true;
			txtComAmountTf.visible = true;
			txtValueComAmountTf.visible = true;
			txtTotalTf.visible = true;
			txtValueTotalTf.visible = true;
		}
	}

}